package com.unsins.core.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: odpsoft
 * Date: 2008-11-27
 * Time: 11:04:59
 */
public final class WebUtils {
    private WebUtils() {
    }

    /**
     * 重载Spring WebUtils中的函数,作用如函数名所示 加入泛型转换,改变输入参数为request 而不是session
     *
     * @param name  session中变量名称
     * @param clazz session中变量的类型
     */
    @SuppressWarnings("unchecked")
    public static <T> T getOrCreateSessionAttribute(
            final HttpServletRequest request, String name, Class<T> clazz) {
        return (T) org.springframework.web.util.WebUtils
                .getOrCreateSessionAttribute(request.getSession(), name, clazz);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getSessionAttribute(final HttpServletRequest request,
                                            String name) {
        return (T) org.springframework.web.util.WebUtils.getSessionAttribute(
                request, name);
    }

    public static void createSessionAttribute(final HttpServletRequest request,
                                              String name, Object value) {
        org.springframework.web.util.WebUtils.setSessionAttribute(request,
                name, value);
    }

    public static void removeSessionAttribute(final HttpServletRequest request,
                                              String name) {
        HttpSession session = request.getSession();
        session.removeAttribute(name);
        session.invalidate();
    }

}
